home *** CD-ROM | disk | FTP | other *** search
/ Kellogg's Amérique / Kellogg's Amérique / main.swf / scripts / fl / controls / BaseButton.as next >
Text File  |  2020-08-04  |  6KB  |  224 lines

  1. package fl.controls
  2. {
  3.    import fl.core.InvalidationType;
  4.    import fl.core.UIComponent;
  5.    import fl.events.ComponentEvent;
  6.    import flash.display.DisplayObject;
  7.    import flash.events.MouseEvent;
  8.    import flash.events.TimerEvent;
  9.    import flash.utils.Timer;
  10.    
  11.    public class BaseButton extends UIComponent
  12.    {
  13.       
  14.       private static var defaultStyles:Object = {
  15.          "upSkin":"Button_upSkin",
  16.          "downSkin":"Button_downSkin",
  17.          "overSkin":"Button_overSkin",
  18.          "disabledSkin":"Button_disabledSkin",
  19.          "selectedDisabledSkin":"Button_selectedDisabledSkin",
  20.          "selectedUpSkin":"Button_selectedUpSkin",
  21.          "selectedDownSkin":"Button_selectedDownSkin",
  22.          "selectedOverSkin":"Button_selectedOverSkin",
  23.          "focusRectSkin":null,
  24.          "focusRectPadding":null,
  25.          "repeatDelay":500,
  26.          "repeatInterval":35
  27.       };
  28.        
  29.       
  30.       protected var _selected:Boolean = false;
  31.       
  32.       private var unlockedMouseState:String;
  33.       
  34.       protected var pressTimer:Timer;
  35.       
  36.       protected var mouseState:String;
  37.       
  38.       protected var background:DisplayObject;
  39.       
  40.       private var _mouseStateLocked:Boolean = false;
  41.       
  42.       protected var _autoRepeat:Boolean = false;
  43.       
  44.       public function BaseButton()
  45.       {
  46.          _selected = false;
  47.          _autoRepeat = false;
  48.          _mouseStateLocked = false;
  49.          super();
  50.          buttonMode = true;
  51.          mouseChildren = false;
  52.          useHandCursor = false;
  53.          setupMouseEvents();
  54.          setMouseState("up");
  55.          pressTimer = new Timer(1,0);
  56.          pressTimer.addEventListener(TimerEvent.TIMER,buttonDown,false,0,true);
  57.       }
  58.       
  59.       public static function getStyleDefinition() : Object
  60.       {
  61.          return defaultStyles;
  62.       }
  63.       
  64.       protected function endPress() : void
  65.       {
  66.          pressTimer.reset();
  67.       }
  68.       
  69.       public function set mouseStateLocked(param1:Boolean) : void
  70.       {
  71.          _mouseStateLocked = param1;
  72.          if(param1 == false)
  73.          {
  74.             setMouseState(unlockedMouseState);
  75.          }
  76.          else
  77.          {
  78.             unlockedMouseState = mouseState;
  79.          }
  80.       }
  81.       
  82.       public function get autoRepeat() : Boolean
  83.       {
  84.          return _autoRepeat;
  85.       }
  86.       
  87.       public function set autoRepeat(param1:Boolean) : void
  88.       {
  89.          _autoRepeat = param1;
  90.       }
  91.       
  92.       override public function set enabled(param1:Boolean) : void
  93.       {
  94.          super.enabled = param1;
  95.          mouseEnabled = param1;
  96.       }
  97.       
  98.       public function get selected() : Boolean
  99.       {
  100.          return _selected;
  101.       }
  102.       
  103.       protected function mouseEventHandler(param1:MouseEvent) : void
  104.       {
  105.          if(param1.type == MouseEvent.MOUSE_DOWN)
  106.          {
  107.             setMouseState("down");
  108.             startPress();
  109.          }
  110.          else if(param1.type == MouseEvent.ROLL_OVER || param1.type == MouseEvent.MOUSE_UP)
  111.          {
  112.             setMouseState("over");
  113.             endPress();
  114.          }
  115.          else if(param1.type == MouseEvent.ROLL_OUT)
  116.          {
  117.             setMouseState("up");
  118.             endPress();
  119.          }
  120.       }
  121.       
  122.       public function setMouseState(param1:String) : void
  123.       {
  124.          if(_mouseStateLocked)
  125.          {
  126.             unlockedMouseState = param1;
  127.             return;
  128.          }
  129.          if(mouseState == param1)
  130.          {
  131.             return;
  132.          }
  133.          mouseState = param1;
  134.          invalidate(InvalidationType.STATE);
  135.       }
  136.       
  137.       protected function startPress() : void
  138.       {
  139.          if(_autoRepeat)
  140.          {
  141.             pressTimer.delay = Number(getStyleValue("repeatDelay"));
  142.             pressTimer.start();
  143.          }
  144.          dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN,true));
  145.       }
  146.       
  147.       protected function buttonDown(param1:TimerEvent) : void
  148.       {
  149.          if(!_autoRepeat)
  150.          {
  151.             endPress();
  152.             return;
  153.          }
  154.          if(pressTimer.currentCount == 1)
  155.          {
  156.             pressTimer.delay = Number(getStyleValue("repeatInterval"));
  157.          }
  158.          dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN,true));
  159.       }
  160.       
  161.       public function set selected(param1:Boolean) : void
  162.       {
  163.          if(_selected == param1)
  164.          {
  165.             return;
  166.          }
  167.          _selected = param1;
  168.          invalidate(InvalidationType.STATE);
  169.       }
  170.       
  171.       override public function get enabled() : Boolean
  172.       {
  173.          return super.enabled;
  174.       }
  175.       
  176.       override protected function draw() : void
  177.       {
  178.          if(isInvalid(InvalidationType.STYLES,InvalidationType.STATE))
  179.          {
  180.             drawBackground();
  181.             invalidate(InvalidationType.SIZE,false);
  182.          }
  183.          if(isInvalid(InvalidationType.SIZE))
  184.          {
  185.             drawLayout();
  186.          }
  187.          super.draw();
  188.       }
  189.       
  190.       protected function setupMouseEvents() : void
  191.       {
  192.          addEventListener(MouseEvent.ROLL_OVER,mouseEventHandler,false,0,true);
  193.          addEventListener(MouseEvent.MOUSE_DOWN,mouseEventHandler,false,0,true);
  194.          addEventListener(MouseEvent.MOUSE_UP,mouseEventHandler,false,0,true);
  195.          addEventListener(MouseEvent.ROLL_OUT,mouseEventHandler,false,0,true);
  196.       }
  197.       
  198.       protected function drawLayout() : void
  199.       {
  200.          background.width = width;
  201.          background.height = height;
  202.       }
  203.       
  204.       protected function drawBackground() : void
  205.       {
  206.          var _loc1_:* = null;
  207.          var _loc2_:DisplayObject = null;
  208.          _loc1_ = !!enabled ? mouseState : "disabled";
  209.          if(selected)
  210.          {
  211.             _loc1_ = "selected" + _loc1_.substr(0,1).toUpperCase() + _loc1_.substr(1);
  212.          }
  213.          _loc1_ += "Skin";
  214.          _loc2_ = background;
  215.          background = getDisplayObjectInstance(getStyleValue(_loc1_));
  216.          addChildAt(background,0);
  217.          if(_loc2_ != null && _loc2_ != background)
  218.          {
  219.             removeChild(_loc2_);
  220.          }
  221.       }
  222.    }
  223. }
  224.